#!/bin/bash
set -e

timestamp=$(date +%s%3N)

workDir=`cd $(dirname $0); pwd`
workDir=${workDir}/${timestamp}
mkdir -p $workDir
echo "$workDir"
cd $workDir


# git地址
gitUrl=git@172.18.0.88:160612/mining-platform.git
# 分支
branch=dev
echo "git clone $gitUrl"
git clone -b $branch $gitUrl

cd mining-platform

# 运行容器并获取容器ID
container_id=$(docker run -d --name mvn-$timestamp \
    -v $workDir/mining-platform:/workspace/mining-platform \
    -v /root/.m2/repository:/root/.m2/repository \
    -v /opt/maven/conf/settings.xml:/opt/maven/conf/settings.xml \
    mvn:jdk21 \
    /bin/sh -c "cd /workspace/mining-platform && /bin/sh /opt/maven/bin/mvn clean verify sonar:sonar -Dmaven.test.skip=true -Dsonar.projectKey=hy-mining -Dsonar.host.url=http://10.6.64.142:30090/  -Dsonar.login=sqp_3482dc3caa8c452c2b5367405e23026a803ef27c")

# 检查容器是否已经运行成功
if [ -z "$container_id" ]; then
    echo "Failed to start the container."
    exit 1
fi


# 实时输出容器日志
docker logs -f $container_id &

# 等待容器执行完毕
docker wait $container_id

# 获取容器退出状态码
exit_code=$?

# 等待日志输出子进程结束
wait

# 根据退出状态码进行不同的处理
docker rm $container_id

case $exit_code in
  0)
    echo "maven executed successfully."
    # 执行成功后的操作
    ;;
  1)
    echo "maven encountered an error."
    # 处理错误情况
    ;;
  *)
    echo "maven Unknown exit code: $exit_code"
    # 处理其他未知情况
    ;;
esac

